<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" append-to-body>
    <el-form ref="modalForm" :model="dataSource" status-icon>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item v-if="modalType === 'edit'" label="楼号编号" :label-width="formLabelWidth">
            <el-input v-model="dataSource.id" disabled autocomplete="off" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="楼号："
            prop="buildingNumber"
            :label-width="formLabelWidth"
            :rules="[
              { required: true, message: '楼号不能为空',trigger:'blur,change'},
              { min: 1,max:3, message: '长度在1-3个字符之间',trigger:'change'}
            ]"
          >
            <el-input v-model="dataSource.buildingNumber" placeholder="请输入楼号 例：（ 42 ）" autocomplete="off" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="选择小区："
            prop="villageCode"
            :label-width="formLabelWidth"
            :rules="[
              { required: true, message: '小区信息不能为空',trigger:'blur,change'}
            ]"
          >
            <el-select
              v-model="dataSource.villageCode"
              style="position: relative;left: 5px"
              filterable
              default-first-option
              placeholder="请选择小区信息"
            >
              <el-option
                key="0"
                label=""
                value=""
              />
              <el-option
                v-for="item in villageList"
                :key="item.villageId"
                :label="item.villageName"
                :value="item.villageId"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleConfirm('modalForm')">确 定</el-button>
    </div>
  </el-dialog>
</template>
<style scoped>
.down-tree{
  height: 350px;
  display: block;
  overflow-y: scroll;
}
</style>
<script>
import { getAllVillageInfoList } from '@/api/ht/xqzhgl/xqgl/xqxxgl'
const { Message } = require('element-ui')
const { modifyOneBuildingInfo, addOneBuildingInfo } = require('@/api/ht/xqzhgl/lhgl/lhxxgl')
export default {
  name: 'Modal',
  props: {
    buildingInfo: {
      type: Object,
      default: () => {
        return {
          id: '',
          buildingNumber: '',
          villageCode: ''
        }
      }
    },
    villageCode: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      title: '楼号信息',
      formLabelWidth: '150px', // 表单输入框字段宽度
      dialogFormVisible: false, // 对话框是否显示
      modalType: 'add',
      dataSource: { // 数据源
      },
      villageList: [], // 小区数据列表
      defaultProps: {
        children: 'children',
        label: 'value'
      }
    }
  },
  watch: {
    buildingInfo(newdata, prevdata) {
      this.dataSource = newdata
      this.title = '楼号信息修改' + this.dataSource.id
    }
  },
  methods: {
    checkModelType(type) {
      this.modalType = type
      // 展示对话框
      this.dialogFormVisible = true
      // 判断是编辑还是新增
      this.getVillageInfoListHandle()
      if (type === 'add') {
        // 置空
        this.title = '楼号信息新增'
        this.hasSelectedCotyObj = {}
        console.log(this.villageCode)
        this.dataSource = { villageCode: this.villageCode }
      }
    },
    handleConfirm(formName) {
      const type = this.modalType
      // 判断是编辑还是新增
      if (type === 'add') {
        // 调用新增请求
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.addBuildingInfoHandle()
          } else {
            console.log('表单缺少数据或者不符合要求')
            return false
          }
        })
      } else {
        // 调用修改请求
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.modifyBuildingInfoHandle()
          } else {
            console.log('表单缺少数据或者不符合要求')
            return false
          }
        })
      }
    },
    getVillageInfoListHandle() {
      this.getVillageInfoList()
    },
    addBuildingInfoHandle() {
      this.addBuildingInfo()
    },
    modifyBuildingInfoHandle() {
      this.modifyBuildingInfo()
    },
    async getVillageInfoList() {
      const res = await getAllVillageInfoList()
      const data = res.data.main
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        this.villageList = data
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    async addBuildingInfo() {
      const res = await addOneBuildingInfo(this.dataSource)
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        Message({
          message: resp.msg || 'Error',
          type: 'success',
          duration: 2 * 1000
        })
        this.dialogFormVisible = false
        this.reflushDataHandle()
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    async modifyBuildingInfo() {
      const res = await modifyOneBuildingInfo(this.dataSource)
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        Message({
          message: resp.msg || 'Error',
          type: 'success',
          duration: 2 * 1000
        })
        this.dialogFormVisible = false
        this.reflushDataHandle()
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    reflushDataHandle() {
      // 调用父组件获取方法刷新数据
      this.$emit('reflushDataHandle')
    }
  }
}
</script>

<style scoped>

</style>
